package lz.controls
{
	import com.greensock.TweenLite;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.MouseEvent;

	/**
	 * 横向滚动 
	 * 
	 * var hScroll:HScroll = new HScroll();
	 * hScroll.setBtn(leftBtn,rightBtn);//设置左右按钮
	 * hScroll.setWidth(width);			//设置遮罩的宽
	 * hScroll.speed = speed;			//设置滚动量
	 * hScroll.delayedTime = delayedTime//滚动时间 
	 * hScroll.target = target			//滚动目标 
	 * 
	 * @author Administrator
	 * 
	 */	
	public class HScroll extends Sprite
	{
		/**
		 *移动大小 
		 */
		private var _spreed:int = 10;
		/**
		 *内容显示的宽 
		 */
		private var _showWidth:int;
		/**
		 *内容显示的高 
		 */
		private var _showHeight:int;
		/**
		 *滚动时间  
		 */
		private var _delayedTime:Number = 1;
		/**
		 *内容容器 
		 */
		private var _container:Sprite;
		/**
		 *左按钮 
		 */
		private var _left:LButton;
		/**
		 *右按钮 
		 */
		private var _right:LButton;
		/**
		 *内容 
		 */
		private var _target:Sprite;
		/**
		 *当前值 
		 */
		private var _current:int = 0;
		/**
		 *默认容器 
		 */
		private var _default:MovieClip;
		
		public function HScroll()
		{
			super();
			init();
		}
		/**
		 * 
		 * 
		 */
		private function init():void
		{
			_container = new Sprite();
			this.addChild(_container);
		}
		/**
		 * 设置显示内容宽 
		 * @param $width
		 * @param $height
		 * 
		 */
		public function setWidth($width:int):void
		{
			_showWidth = $width;
		}
		/**
		 *设置显示内容高度 
		 * @param $height
		 * 
		 */
		public function setHeight($height:int):void
		{
			_showHeight = $height;
		}
		/**
		 *设置左右按钮 
		 * @param $left
		 * @param $right
		 * 
		 */
		public function setBtn($left:MovieClip,$right:MovieClip):void
		{
			_left = new LButton($left);
			_left.enable = true;
			_right = new LButton($right);
			_right.enable = true;
//			this.addChild(_left);
//			this.addChild(_right);
			addEvent();
//			_left.y = 0;
//			_right.y = 0;
		}
		/**
		 *移动大小（滚动大小、多少） 
		 * @param $value
		 * 
		 */
		public function set speed($value:int):void
		{
			_spreed = $value;
		}
		/**
		 * 设置滚动时间 
		 * @param $value
		 * 
		 */
		public function set delayedTime($delayedTime:Number):void
		{
			_delayedTime = $delayedTime;
		}
		/**
		 *设置目标  
		 * @param $target
		 * 
		 */
		public function set target($target:Sprite):void
		{
			_target = $target;
			_container.addChild(_target);
			_left.x = 0;
			_container.x = _left.x + _left.width +10 
			_right.x = _container.x + _showWidth + 20;
			var shape:Shape = new Shape();
			shape.graphics.clear();
			shape.graphics.beginFill(0xFFFFFF);
			shape.graphics.drawRect(0,-10,_showWidth ,_target.height + 10);
			shape.graphics.endFill();
			_container.addChild(shape);
			_container.mask = shape;
			_left.y = _right.y = ( _target.height - _left.height)>>1;
			
		}
		/**
		 *设置默认容器 
		 * @param $Default
		 * 
		 */
		public function set defaultContain($Default:MovieClip):void
		{
			_default = $Default;
		}
		/**
		 *设置默认容器目标 
		 * @param $target
		 * 
		 */
		public function set defaultTarget($target:Sprite):void
		{
			_target = $target;
			_default.addChild(_target);
			var shape:Shape = new Shape();
			shape.graphics.clear();
			shape.graphics.beginFill(0xFFFFFF);
			shape.graphics.drawRect(_target.x,_target.y,_showWidth ,_target.height <= 0?_showHeight:_target.height + 10);
			shape.graphics.endFill();
			_default.mask = shape;
			_default.addChild(shape);
			
		}
		/**
		 * 
		 * 
		 */
		public function addEvent():void
		{
			_right.addEventListener(MouseEvent.CLICK,onRightHandler);
			_left.addEventListener(MouseEvent.CLICK,onLeftHandler);
		}
		/**
		 *向左移动 
		 * @param event
		 * 
		 */
		private function onRightHandler(event:MouseEvent):void  
		{
			if((-_current *_spreed  + _showWidth)>= (_target.numChildren) * _spreed)return; 
			_current--;
//			removeEvent();
			TweenLite.killTweensOf(_target);
			TweenLite.to(_target,_delayedTime,{x:_current *_spreed ,onComplete:onComplete});
			upData();
		}
		/**
		 * 向右移动 
		 * @param event
		 * 
		 */
		private function onLeftHandler(event:MouseEvent):void
		{
			if(_current *_spreed >= 0)return;
			_current++;
//			removeEvent();
			var temp:int;
			TweenLite.killTweensOf(_target);
			TweenLite.to(_target,_delayedTime,{x:_current *_spreed ,onComplete:onComplete});
			upData();
		}
		/**
		 *当前 
		 * @param $value
		 * 
		 */
		public function set current($value:int):void
		{
//			_current = $value;
			_current = (_target.x / _spreed);
		}
		/**
		 *缓动完成 
		 * 
		 */
		private function onComplete():void
		{
			addEvent();
		}
		public function removeEvent():void
		{
			_left.removeEventListener(MouseEvent.CLICK,onLeftHandler);
			_right.removeEventListener(MouseEvent.CLICK,onRightHandler);
		}
		/**
		 *更新 
		 * 
		 */
		public function upData():void
		{
			if((-_current *_spreed  + _showWidth)>= (_target.numChildren) * _spreed)
			{
				_right.enable = false;
				_right.filter = true;
			}else
			{
				_right.enable = true;
				_right.filter = false;
			}
			if(_current *_spreed ==0)
			{
				_left.enable = false;
				_left.filter = true;
			}else
			{
				_left.enable = true;
				_left.filter = false;
			}
//			trace(_target.x / _spreed + "-->"+ _current);
		}
		public function destroy():void
		{
			TweenLite.killTweensOf(_target);
			removeEvent();
			_container=null;
			_left.filter = false;
			_right.filter = false;
			_left.enable = false;
			_right.enable = false;
			_left = null;
			_right = null;
			_default = null;
			
			_target = null;
		}
	}
}